package com.CGFrame.Axis;

import java.util.Iterator;

import org.apache.axis.MessageContext;
import org.apache.axis.session.Session;
import org.jdom.Document;
import org.jdom.Element;

import com.CGFrame.struts.POJOs.Confirmhistory;
import com.CGFrame.struts.bean.confirm.ConfirmHistoryBean;
import com.CGFrame.struts.bean.task.TaskBean;
import com.CGFrame.struts.bean.unit.FilePathGenerator;

public class TaskDetail {
	@SuppressWarnings("deprecation")
	public String getTaskDetail(String taskId)
	{
		try
		{
			MessageContext mc = MessageContext.getCurrentContext();
			Session session = mc.getSession();
			String regUserId = (String)session.get("RegUserId");
			if (regUserId==null)
			{
				return null;
			}
			
			TaskBean taskBean = new TaskBean( Integer.parseInt(taskId) );
			Element elementRoot = new Element("Task");
			elementRoot.addAttribute("TaskId",taskId);
			elementRoot.addAttribute("TaskName",taskBean.getTaskName());
			elementRoot.addAttribute("TaskDescription",taskBean.getTaskDescription());
			elementRoot.addAttribute("Status",taskBean.getStatusName()+"");
			Integer statusId = taskBean.getStatusId();
			elementRoot.addAttribute("StatusID",statusId.toString());
			if (statusId == 3  ) //isdoing
			{
				elementRoot.addAttribute("MediaTargetPath",taskBean.getFilePathforTask(FilePathGenerator.MediaFile));
				elementRoot.addAttribute("MakeTargetPath",taskBean.getFilePathforTask(FilePathGenerator.MakeFile));
				elementRoot.addAttribute("TargetFileName",taskBean.getFileName(FilePathGenerator.MakeFile,null, false).toString()) ;//commitFileName without ext
			}
			else if (statusId == 6 ||statusId == 7)// confirmpass or finished
			{
				elementRoot.addAttribute("MediaTargetPath",taskBean.getFilePathforTask(FilePathGenerator.FinishedMediaFile));
				elementRoot.addAttribute("MakeTargetPath",taskBean.getFilePathforTask(FilePathGenerator.FinishedMakeFile));
				elementRoot.addAttribute("TargetFileName",taskBean.getFileName(FilePathGenerator.FinishedMakeFile,null, false).toString()) ;//commitFileName without ext
			}
			
			elementRoot.addAttribute("PlanStartTime",taskBean.getVe().toLocaleString()+"" );
			if (regUserId.equals(taskBean.getTask().getReguser().getRegUserId().toString() ) )
			{
				elementRoot.addAttribute("SelfFlag","True");
			}
			
	
			Document document = new Document(elementRoot);
			
			
			ConfirmHistoryBean cHB = new ConfirmHistoryBean();
			Iterator ite_ConfirmHistory  = cHB.getTaskConfirmHistroty(taskBean.getTaskId().toString()).iterator();
			while (ite_ConfirmHistory.hasNext())
			{
				Confirmhistory ch = (Confirmhistory)ite_ConfirmHistory.next();
				Element element = new Element("Confirmhistory");
				element.addAttribute("RegName",ch.getReguser().getRegName());
				element.addAttribute("Time",ch.getConfirmTime().toLocaleString());
				element.addAttribute("Information",ch.getConfirmInformation()+"");
				if(ch.getIsPass()!=null)
				{
					if(ch.getIsPass()==1) element.addAttribute("IsPass","Pass");
					else if (ch.getIsPass()==0) element.addAttribute("IsPass","NotPass");
				}
				else element.addAttribute("IsPass","");
				document.getRootElement().addContent(element);
			}
			
			
			System.out.println(XMLOut.outputToString(document));
			return XMLOut.outputToString(document);
		}
		catch (Exception e)
		{
			return null;
		}
	}
	
	public String setIsDoing(String taskId)
	{
		MessageContext mc = MessageContext.getCurrentContext();
		Session session = mc.getSession();
		String regUserId = (String)session.get("RegUserId");
		if (regUserId==null)
		{
			return null;
		}
		TaskBean taskBean = new TaskBean(Integer.parseInt(taskId));
		taskBean.setIsDoing();
		return "SUCCESS";
	}
	
	public String commitFile(String taskId)
	{
		try
		{

			MessageContext mc = MessageContext.getCurrentContext();
			Session session = mc.getSession();
			String regUserId = (String)session.get("RegUserId");
			if (regUserId==null)
			{
				return null;
			}
			TaskBean taskBean  = new TaskBean(Integer.parseInt(taskId));
			int statusId = taskBean.getTask().getStatus().getStatusId(); 
			// view  id at  table "database.status"
			if (statusId == 3) //isdoing
			{
				taskBean.setWaitingConfirm();
				ConfirmHistoryBean cHB = new ConfirmHistoryBean();
				cHB.AddTaskConfirmHistory(taskId, regUserId, null, "To Check");
			}
			else if (statusId == 6 || statusId == 7) //confirmPass or finished
			{
				taskBean.setFinish();
				ConfirmHistoryBean cHB = new ConfirmHistoryBean();
				cHB.AddTaskConfirmHistory(taskId, regUserId, null, "Finished");
			}
			return "SUCCESS";
		}
		catch(Exception e)
		{
			System.out.println(e);
			return null;
		}
	}
	
	public String readForCommit(String taskId)
	{
		try
		{
			MessageContext mc = MessageContext.getCurrentContext();
			Session session = mc.getSession();
			String regUserId = (String)session.get("RegUserId");
//			regUserId = null;
			if (regUserId==null)
			{
				return "Please Login";

			}
			TaskBean taskBean  = new TaskBean(Integer.parseInt(taskId));
			int statusId = taskBean.getTask().getStatus().getStatusId();
			if (statusId == 6 || statusId == 7 ||statusId==3)
				return "SUCCESS";
			else 
			{
				return "Status Run";
			}
		}
		catch(Exception e)
		{
		//	System.out.println(e);
			return e.toString();
		}
	}
}
